package test.movies;

import java.util.NoSuchElementException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import static org.openqa.selenium.support.PageFactory.initElements;
import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;


import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginPage extends PageMovies {
	

    // Logger variable
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(LoginPage.class);

    // Url of Movies server
    private String moviesServerUrl;
    
    // Field with user name on Login page 
    private String fieldUserName =  "//input[@id='username']";

    // Field with password on Login page 
    private String fieldPassword =  "//input[@name='password']";

    // Button Submit on Login page 
    private String fieldLogIn =  "//input[@name='submit']";

    public LoginPage (WebDriver driver, String moviesServerUrl) {
        super(driver);
        this.moviesServerUrl = moviesServerUrl;
        initElements(driver, this);
    }
    
    public LoginPage (WebDriver driver){
        super(driver);
        initElements(driver, this);
    }
    
    /**
     * Method will open Login page
     */
    public void openMoviesServer() {
        LOGGER.info("Opening Movies url");	    	
        driver.get(moviesServerUrl);
    }
    
    /**
     * Check if it is the right page
     * @return true if right page
     */
    

    public boolean checkIfLoginPageIsOpen() {
        LOGGER.info("Checking whether the user on the Login Page");

        try {
            WebElement checkPage = new WebDriverWait(driver,20).until(visibilityOfElementLocated(By.xpath(fieldUserName)));
            LOGGER.info("Login Page is open");

            return true;
                                       
    }
    catch (NullPointerException e){
        LOGGER.error("Login Page is disable");
        return false;
    }
    catch (TimeoutException e){
        LOGGER.error("Login page is not open!!!!");
        return false;}
    }
    
    /**
     * this method enters login and password, logs in the application
     * @param name
     * @param password
     * @return
     */
    public MainPage authorization(String name, String password) {

        LOGGER.info("Execute Log in");

        try {

            WebElement nameInsert = wait.until(elementToBeClickable(By.xpath(fieldUserName)));
            LOGGER.info("Insert Login");
            nameInsert.sendKeys(Keys.CONTROL + "a");
            nameInsert.sendKeys(Keys.DELETE);
            nameInsert.sendKeys(name);

            WebElement passwordInsert = wait.until(elementToBeClickable(By.xpath(fieldPassword)));
            LOGGER.info("Insert Password");
            passwordInsert.sendKeys(Keys.CONTROL + "a");
            passwordInsert.sendKeys(Keys.DELETE);
            passwordInsert.sendKeys(password);
            
            WebElement buttonLogIn = wait.until(elementToBeClickable(By.xpath(fieldLogIn)));
            LOGGER.info("Log in ");

            buttonLogIn.click(); 
           
  
            return new MainPage(driver);
        }
        catch (NullPointerException e){
            LOGGER.error("Login Page is disable");
            return null;
        }
        catch (TimeoutException e){
            LOGGER.error(String.format("Error while authorization: Can't find fields to authorize. Field of Login Page is not found",e.toString()));
            return null;
        }

    }

    
    


}
